<?php
declare (strict_types=1);

namespace app\admin\controller;

use think\App;
use app\common\model\Role;
use addons\Builder\facade\Builder;
use think\exception\ValidateException;
use think\facade\View;
use think\response\Json;

class Admin extends Base
{
    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\Admin';

    //验证器
    protected string $validate = 'Admin';

    //关联模型
    protected array $hasOne = ['role'];

    public function index(): string
    {
        //构建table
        return Builder::Table()
            ->addColumn(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addColumn(['field' => 'admin_name', 'title' => '用户名', 'modify' => '1,input'])
            ->addColumn(['field' => 'mobile', 'title' => '手机', 'modify' => '2,input'])
            ->addColumn(['field' => 'role_id', 'title' => '角色', 'modify' => '2,select,getRole', 'templet' => '<span>{{d.role?d.role.title:"暂无"}}</span>'])
            ->addColumn(['field' => 'comment', 'title' => '备注', 'modify' => '2,input'])
            ->addColumn(['field' => 'password', 'title' => '登录密码', 'modify' => '2,password', 'hide' => 'false'])
            ->addColumn(['field' => 'create_time', 'title' => '添加时间'])
            ->addColumn(['field' => 'last_time', 'title' => '最后登录时间'])
            ->addColumn(['field' => 'status', 'title' => '状态', 'templet' => '#status', 'width' => 120])
            ->addColumn(['field' => 'is_lock', 'title' => '权限', 'templet' => '#is_lock', 'width' => 120])
            ->addColumn(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#adminBarDemo', 'title' => '操作'])
            ->addTopButtons(['add' => ['size' => '50%']])
            ->addRightButtons(['edit' => ['size' => '50%'], 'delete'])
            ->addSwitchTool([
                ['name' => 'status', 'pk' => $this->pk, 'text' => '正常|锁定'],
                ['name' => 'is_lock', 'pk' => $this->pk, 'text' => '有权限|无权限']
            ])
            ->addSearch([
                ['admin_name__mobile__comment', '用户名/手机/备注', 'input,Like'],
                ['role_id', '角色类型', 'select', self::getRole()]
            ])
            ->addExtraHtml(['admin'])
            ->fetch();
    }

    public static function getRole(): array
    {
        return Role::column('title', 'id');
    }


    public function save($param = [], $model = true): Json
    {
        $param = $this->request->post();

        if (isset($param['password']) && $param['password']) {
            $param['password'] = md5($param['password']);
        }
        return parent::save($param, $model); // TODO: Change the autogenerated stub
    }


    public function password()
    {
        if ($this->request->isPost()) {
            $param = $this->request->post();

            try {
                $this->validate($param, 'AdminPassword');
            } catch (ValidateException $e) {
                return json(['code' => -1, 'msg' => $e->getError()]);
            }

            if (md5($param['old']) != $this->admin->password) {
                return json(['code' => -1, 'msg' => '原密码错误']);
            }

            $this->model::update(['password' => md5($param['password'])], ['id' => $this->admin->id]);

            \app\common\model\Logs::adds('管理员【' . $this->admin->admin_name . '】修改了密码');
            session('admin_id', null);

            return json(['code' => 1, 'msg' => '已修改']);
        }
        return View::fetch();
    }

}
